package com.mallcai.bigdata.ladon.dpl.entity.common;

import com.mallcai.bigdata.ladon.dpl.entity.BaseEntity;
import com.mallcai.bigdata.ladon.dpl.vo.DatasourceType;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class Datasource extends BaseEntity {
    /**
     * 数据源类型
     *
     * @see DatasourceType
     */
    private String  type;
    /**
     * 名称
     */
    private String  name;
    /**
     * 状态, 0 - active; 1 - disabled
     */
    private Integer status;
    /**
     * jdbc驱动
     */
    private String  jdbcDriver;
    /**
     * jdbc URL
     */
    private String  jdbcUrl;
    /**
     * 登录名
     */
    private String  username;
    /**
     * 登录密码
     */
    private String  password;
    /**
     * 连接池超时时间(ms)
     */
    private Integer timeout;
    /**
     * 检测SQL
     */
    private String  testSql;
    /**
     * 使用类型
     */
    private Byte    useType;

    /**
     * 创建人
     */
    private String creator;


    private static final Pattern PATTERN = Pattern.compile("socketTimeout=", Pattern.CASE_INSENSITIVE);

    public String getJdbcUrlWithSocketTimeout() {
        if (DatasourceType.MySQL.getCode().equalsIgnoreCase(type)) {
            Matcher matcher = PATTERN.matcher(this.jdbcUrl);
            if (!matcher.find()) {
                return this.jdbcUrl + "&socketTimeout=60000";
            }
        }
        return this.jdbcUrl;
    }
}
